package com.xiao.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description ES java高级客户端配置类
 * @Auther: 笑笑是一个码农
 * @Date: 14:40 2021/1/16
 */
@Configuration
public class ESHighLevelClientConfig {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.schema}")
    private String schema;


    /**
     * 把RestHighLevelClient放入spring容器，用到的地方可以直接注入
     * @return
     */
    @Bean(name = "esHighLevelRestClient",destroyMethod = "close")
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, schema));
        return new RestHighLevelClient(builder);
    }
}
